package parser;

import java.util.ArrayList;
import java.util.HashMap;

public class ParserToMap {

	public static ArrayList<HashMap<String, ArrayList<String>>> parse(
			String[][] data) {
		ArrayList<HashMap<String, ArrayList<String>>> result = new ArrayList<HashMap<String, ArrayList<String>>>();
		if (data != null && (data.length!=0)) {
			int i = 0;
			while (i < data.length) {
				String idObj = data[i][0];
				String typeObj = data[i][1];
				HashMap<String, ArrayList<String>> propertiesMap = new HashMap<String, ArrayList<String>>();
				propertiesMap.put("id", arrStr(idObj));
				propertiesMap.put("type", arrStr(typeObj));
				while (i < data.length && data[i][0].equalsIgnoreCase(idObj)) {
					ArrayList<String> valuesArr = new ArrayList<String>();
					String property = data[i][2];
					while (i < data.length
							&& data[i][0].equalsIgnoreCase(idObj)
							&& data[i][2].equalsIgnoreCase(property)) {
						String value = data[i][3];
						valuesArr.add(value);
						i++;
					}
					propertiesMap.put(property.toLowerCase(), valuesArr);
				}
				result.add(propertiesMap);
			}
		}
		return result;
	}

	private static ArrayList<String> arrStr(String data) {
		ArrayList<String> result = new ArrayList<String>();
		result.add(data);
		return result;
	}
}